// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_ui_font_render.h"
#include "mk_engine.h"
#include "mk_ui_manager.h"

namespace MK3DE
{
    CUIFontRender::CUIFontRender() :
        _fontIdx(-1),
        _fontColor(0xffffffff),
        _fontBorderType(FBT_BORDER),
        _fontBorderWeight(1),
        _fontBorderColor(0xff000000),
        _xText(0),
        _yText(0),
        _alignTypeX(UIATX_CENTER),
        _alignTypeY(UIATY_CENTER),
        _width(0),
        _height(0)
    {

    }

    CUIFontRender::~CUIFontRender()
    {

    }

    void CUIFontRender::Recalculate()
    {
        if (_alignTypeX == UIATX_LEFT)
            _xText = 0;
        else if (_alignTypeX == UIATX_RIGHT)
            _xText = _size.x - _width;
        else if (_alignTypeX == UIATX_CENTER)
        {
            float center = _size.x * 0.5f;
            _xText = center - _width * 0.5f;
        }

        if (_alignTypeY == UIATY_TOP)
            _yText = 0;
        else if (_alignTypeY == UIATY_BOTTOM)
            _yText = _size.y - _height;
        else if (_alignTypeY == UIATY_CENTER)
        {
            float center = _size.y * 0.5f;
            _yText = center - _height * 0.5f;
        }
    }

    BOOL CUIFontRender::UpdateTextDimension()
    {
        CFTFont* font = GetEngine()->GetUIManager()->GetFont(_fontIdx);
        if (!font)
            return FALSE;

        float width = 0;
        float height = 0;
        if (!font->GetTextDimension(_text.c_str(), (DWORD)_text.size(), &width, &height))
            return FALSE;

        _width = width;
        _height = height;

        return TRUE;
    }

    void CUIFontRender::SetText(const wchar_t* text)
    {
        _text = text;
        if (!UpdateTextDimension())
            return;
        
        Recalculate();
    }

    void CUIFontRender::SetPosition(float x, float y)
    {
        _pos.x = x;
        _pos.y = y;
    }

    void CUIFontRender::SetSize(float sx, float sy)
    {
        _size.x = sx;
        _size.y = sy;
        Recalculate();
    }

    void CUIFontRender::SetFont(int idx)
    {
        if (_fontIdx != idx)
        {
            _fontIdx = idx;
            UpdateTextDimension();
            Recalculate();
        }
        else
            _fontIdx = idx;
    }

    BOOL CUIFontRender::Render()
    {
        CFTFont* font = GetEngine()->GetUIManager()->GetFont(_fontIdx);
        if (!font)
            return FALSE;

        font->SetClipRect(TRUE, _pos.x, _pos.y, _pos.x + _size.x, _pos.y + _size.y);
        font->SetBorderParam(_fontBorderType, _fontBorderColor, _fontBorderWeight);
        font->DrawText((int)_xText + (int)_pos.x, (int)_yText + (int)_pos.y, _fontColor, _text.c_str(), (DWORD)_text.size());
        font->SetClipRect(FALSE, 0, 0, 0, 0);
        return TRUE;
    }
}